﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ES=Entity.SingleChain;

namespace Bussiness.SingleChain
{
    public class BaseOperate
    {
        /// <summary>
        /// 向链表添加一个元素e
        /// </summary>
        /// <param name="list"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public int Add(ES.Node list, ES.Node e)
        {
            if (list == null || e == null)
            {
                return -1;
            }
            ES.Node p = list;
            while (p.Next != null)
            {
                p = p.Next;
            }
            p.Next = e;
            return 1;
        }

        /// <summary>
        /// 将已知元素n插入到链表，成为第n个索引
        /// </summary>
        /// <param name="list"></param>
        /// <param name="n"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public int Insert(ES.Node list, int n, ES.Node e)
        {
            if (list == null || n < 0 || e == null)
            {
                return -1;
            }
            ES.Node p = list;
            int i = 0;
            while (p.Next != null)
            {
                if (n == i)
                {
                    e.Next = p.Next;
                    p.Next = e;
                    return 1;
                }
                p = p.Next;
                i++;
            }
            return -1;
        }

        /// <summary>
        /// 把元素e插入到元素p的后面
        /// </summary>
        /// <param name="p"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public int Insert(ES.Node p, ES.Node e)
        {
            if (p == null || p.Next == null || e == null)
            {
                return -1;
            }
            e.Next = p.Next;
            p.Next = e;
            return 1;
        }

        /// <summary>
        /// 删除链表中的第n个元素
        /// </summary>
        /// <param name="list"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public int Delete(ES.Node list, int n)
        {
            if (list == null || n < 0)
            {
                return -1;
            }
            ES.Node p = list;
            int i = 0;
            while (p.Next != null)
            {
                if (n == i)
                {
                    ES.Node d = p.Next;
                    p.Next = d.Next;
                    return 1;
                }
                p = p.Next;
                i++;
            }
            return -1;
        }

        /// <summary>
        /// 删除元素p后面的元素
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public int Delete(ES.Node p)
        {
            if (p == null || p.Next == null)
            {
                return -1;
            }
            ES.Node d = p.Next;
            p.Next = d.Next;
            return 1;
        }

        /// <summary>
        /// 根据索引获取一个元素，索引从0开始
        /// </summary>
        /// <param name="list"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static ES.Node GetElement(ES.Node list, int n)
        {
            if (list == null || n < 0)
            {
                return null;
            }
            ES.Node p = list;
            int i = 0;
            while (p.Next != null)
            {
                if (n == i)
                {
                    return p.Next;
                }
                p = p.Next;
                i++;
            }
            return null;
        }

        /// <summary>
        /// 获取链表长度
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int Length(ES.Node list)
        {
            if (list == null)
            {
                return 0;
            }
            int i = 0;
            ES.Node p = list;
            while (p.Next != null)
            {
                i++;
                p = p.Next;
            }
            return i;
        }

        /// <summary>
        /// 获取链表字符
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static String GetString(ES.Node list)
        {
            if (list != null)
            {
                StringBuilder listString = new StringBuilder();
                int i = 1;
                ES.Node p = list;
                listString.Append(Length(list) + "|| ");
                while (p.Next != null)
                {
                    // listString.Append(i + "::" + p.Next.Num + "--  ");
                    listString.Append(p.Next.Num + "--  ");
                    i++;
                    p = p.Next;
                }
                return listString.ToString();
            }
            return "";
        }
        public static ES.Node Clone(ES.Node list)
        {
            if (list == null)
            {
                return null;
            }

            ES.Node copyList = new ES.Node();
            ES.Node p = list;
            ES.Node n = copyList;
            while (p.Next != null)
            {
                ES.Node m = new ES.Node(p.Next.Num);
                n.Next = m;
                n = n.Next;
                p = p.Next;
            }
            return copyList;
        }
    }
}
